package com.mmall.controller.admin;

import com.mmall.commons.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.pojo.User;
import com.mmall.service.IProductService;
import com.mmall.util.CheckUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author wgt
 * @date 2018/7/2
 * @project mmall-server
 */
@Controller
@RequestMapping("/manager/product")
public class ManagerProductController {

    @Autowired
    private IProductService productService;

    /**
     * 保存商品信息
     * @param product
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    public ServerResponse saveProduct(Product product, HttpSession session){
        ServerResponse<User> serverResponse = CheckUserUtil.checkRoleAdminNeedLogin(session);
        if (serverResponse.isSuccess()){
            return productService.saveOrUpdateProduct(product);
        }
        return serverResponse;
    }

    /**
     * 更新商品状态
     * @param productId
     * @param status
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/set_sale_status.do", method = RequestMethod.POST)
    public ServerResponse setSaleStatus(Integer productId, Integer status, HttpSession session){
        ServerResponse serverResponse = CheckUserUtil.checkRoleAdminNeedLogin(session);
        if (serverResponse.isSuccess()){
            return productService.setSaleStatus(productId, status);
        }
        return serverResponse;
    }

    @ResponseBody
    @RequestMapping(value = "/detail.do", method = RequestMethod.POST)
    public ServerResponse managerProductDetail(Integer productId, HttpSession session){
        ServerResponse serverResponse = CheckUserUtil.checkRoleAdminNeedLogin(session);
        if (serverResponse.isSuccess()){
            return productService.manageProductDetail(productId);
        }
        return serverResponse;
    }

}
